<?php 
    require_once ('../connect.php');
    //接收图片参数
    $photo_date = $_POST['photo_date'];
    $photo_introduction = $_POST['photo_introduction'];
    $username = $_POST['username'];
    $photo_name =strrchr($_FILES['photo_file']['name'], ".");
    $photo_tmp_name = $_FILES['photo_file']['tmp_name'];
    //赋值user_id
    $user_sql = "select u_id from user where username = '$username'";
    $user_result = $con->query($user_sql);
    $user_row = $user_result->fetch_assoc();
    $user_id = $user_row['u_id'];
    //重命名图片
    $photo_name = $username . $photo_date . $photo_name;
    //获取图片保存的服务器路径
    $ip = $_SERVER['SERVER_ADDR'];
    if($ip == '::1')
        $ip = '127.0.0.1';
    $location = "http://".$ip . "/MDay/picture/$photo_name";
    //保存图片到服务器
    if(file_exists("../picture/$photo_name")){
        $flag = 1;
        echo json_encode($flag);
    }
    else{
        if(move_uploaded_file($photo_tmp_name,"../picture/$photo_name")){
            $add_sql = "insert into photo_wall(location,photo_name,photo_date,introduction,user_id) values('$location','$photo_name','$photo_date','$photo_introduction','$user_id')";
            if($con->query($add_sql)){
                $flag = 0;
                echo json_encode($flag);
            }
            else{
                $flag = 2;
                echo json_encode($flag);
            }
        }
        else{
            $flag = 2;
            echo json_encode($flag);
        }
    }
?>